package com.leqicheng.tenon.shortUrl;

import android.content.ClipboardManager;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.leqicheng.tenon.R;
import com.leqicheng.tenon.R.drawable;
import com.leqicheng.tenon.R.id;
import com.leqicheng.tenon.R.layout;
import com.leqicheng.tenon.login.PopWindow;

public class ShortUrlWindow extends PopWindow {

    private Context mContext;

    private String mShortUrl;

    private PopupWindow pwMyPopWindow;

    public ShortUrlWindow(Context mContext, String mShortUrl) {
        super();
        this.mContext = mContext;
        this.mShortUrl = mShortUrl;

        LayoutInflater inflater = (LayoutInflater) this.mContext
                .getSystemService(this.mContext.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.popwindow_short_url, null);
        LinearLayout parentLayout = (LinearLayout) layout
                .findViewById(R.id.parent_layout);
        TextView shortUrlTv = (TextView) layout
                .findViewById(R.id.popwindow_short_url);
        shortUrlTv.setText(mShortUrl);
        copy(mShortUrl, mContext);
        Toast.makeText(mContext, "地址已经复制到剪贴板上，在浏览器中直接粘贴即可", Toast.LENGTH_SHORT).show();
        ImageButton closeButton = (ImageButton) layout
                .findViewById(R.id.close_window);
        this.pwMyPopWindow = new PopupWindow(layout);
        this.pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件
        parentLayout.measure(View.MeasureSpec.UNSPECIFIED,
                View.MeasureSpec.UNSPECIFIED);
        this.pwMyPopWindow.setWidth(parentLayout.getMeasuredWidth() + 6);
        this.pwMyPopWindow.setHeight(parentLayout.getMeasuredHeight() + 6);
        this.pwMyPopWindow.setBackgroundDrawable(this.mContext.getResources()
                .getDrawable(R.drawable.boder));
        // 控制popupwindow点击屏幕其他地方消失
        this.pwMyPopWindow.setOutsideTouchable(true);//
        // 触摸popupwindow外部，popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功，如上
        closeButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                pwMyPopWindow.dismiss();
            }
        });
    }

    public PopupWindow getPopupWindow() {
        return this.pwMyPopWindow;
    }
    
    public static void copy(String content, Context context)  
    {  
    // 得到剪贴板管理器  
    ClipboardManager cmb = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);  
    cmb.setText(content.trim());
    }  
}
